/**
 * 
 */
package com.ms.game.itembox.usertype;

import com.ms.db.usertype.BasicToString;
import com.ms.game.itembox.ItemBox;
import com.ms.game.itembox.ItemField;
import com.xpec.c4.db.cfg.DBConfig;
import com.xpec.c4.db.usertype.StringArrayParser;
import com.xpec.c4.db.util.CodecTool;

/**
 * @author TW15010
 * 
 */
public class ItemBoxToString extends BasicToString {
	private static final long serialVersionUID = 1260297540258655441L;

	/**
	 * V0 一開始的版本
	 */
	public static final int LATEST_BLOB_VER = 0;

	/**
	 * @see com.ms.db.usertype.BasicToString.equals(Object, Object)
	 */
	@Override
	public boolean equals(Object x, Object y) {
		if (x == y)
			return true;

		if (x != null && y != null) {

			ItemBox o1 = (ItemBox) x;
			ItemBox o2 = (ItemBox) y;

			return o1.equals(o2);
		}

		return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ms.db.usertype.BasicToString#sqlTypes()
	 */
	@Override
	public int[] sqlTypes() {
		return VARCHAR_TYPES;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ms.db.usertype.BasicToString#returnedClass()
	 */
	@Override
	public Class<?> returnedClass() {
		return ItemBox.class;
	}

	// TODO kevin item 完成後檔案可以刪除
	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ms.db.usertype.BasicToString#assemble(java.lang.Object)
	 */
	@Override
	protected String assemble(Object obj) {
		ItemBox box = (ItemBox) obj;

		StringBuilder str = new StringBuilder();

		// version number
		CodecTool.append(str, DBConfig.BLOB_FORMAT_VER_PREFIX + LATEST_BLOB_VER, SPLITTER);

		// item count
		//CodecTool.append(str, box.getItemFields().size(), SPLITTER);
/*
		// all item info
		for (ItemField field : box.getItemFields()) {
			// item id
			CodecTool.append(str, field.getItemId(), SPLITTER);

			// item count
			CodecTool.append(str, field.getCount(), SPLITTER);
		}
*/
		return str.toString();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ms.db.usertype.BasicToString#parse(java.lang.String)
	 */
	@Override
	protected Object parse(String value) {
		ItemBox data = new ItemBox();

		if (value == null || value.equals("")) {
			return data;
		}

		// detect version
		int BlobVer = 0;
		if (value.indexOf(SPLITTER) < 0) {
			BlobVer = 0;
		} else {
			BlobVer = DBConfig.getBlobVer(new StringBuffer(value), SPLITTER);
		}

		// 呼叫對應版本號的 function 將字串還原成 object
		switch (BlobVer) {
		case 0:
			return parse0(value);
		}

		return data;
	}

	protected Object parse0(String value) {
		ItemBox data = new ItemBox();

		if (value == null || value.equals("")) {
			return data;
		}

		// split input string to array
		String[] strArray = CodecTool.split(value, SPLITTER_RE);

		// string parse tool
		StringArrayParser sap = new StringArrayParser(strArray);

		// skip version
		sap.skipOne();

		// item count
		int imax = sap.getInteger();

		// add items
		ItemField field;
		for (int i = 0; i < imax; i++) {
			field = new ItemField();

			// item id
			field.setItemId(sap.getString());

			// item count
			field.setCount(sap.getInteger());

			// add
//			data.getItemFields().add(field);
		}

		return data;
	}
}
